#include <string.h>

int ProcessMessage(int spnum,char *msg,int len){

    int i,j=0,index;

    char *pos1;
    char *pos2;
    int msgnum=0;
    int total = 0;
/*
    printf("\nProc: spnum[%d],len[%d]", spnum, len);
     for(index=0;index<len;index++)
	{
		printf("[%2X] ",msg[index]);
		if(index%16 == 0) printf("\n");
	}
*/
    switch(spnum){
        case 0:// Data between start and first 'r'
            pos1 = strchr(msg,0x0A);

            msgnum= pos1 -msg;
				for(i=0; i<msgnum; i++){
					msg[i] = pos1[i];
				}
            msg[msgnum]=0x00;
		//printf("\nCASE0:pos1[%x], pos2[%x], msgnum[%d]",pos1, pos2,msgnum);
            break;

        case 1:// Data between first and second '\r'
		  case 2: 
        case 3://Data between third and fourth '\r'
            pos1 = strchr(msg,0x0A);
				for(i = 0; i < spnum - 1; i++){
					pos1 = strchr(pos1+1, 0x0A);
				}
            pos2 = strchr(pos1+1,0x0A);
            msgnum= pos2 -pos1-1;

            for(i=0;i<msgnum;i++){

                msg[i]=pos1[i+1];
            }
            msg[msgnum]=0x00;
		//printf("\nCASE3:pos1[%x], pos2[%x], msgnum[%d]",pos1, pos2,msgnum);
            break;

        case 4://PP Summery
		/*
	     for(index=0;index<len;index++)
		{
			printf("[%2X] ",msg[index]);
			if(index%16 == 0) printf("\n");
		}
		*/
		pos1 = strchr(msg,0x0A);
            for(i=0;i<12;i++){
                
                pos2 = strchr(pos1+1,0x0A);
		    //printf("\n [%d] - msgnum[%d], pos1[%d], pos2[%d]", i, msgnum, pos1, pos2);
                msgnum = pos2 -pos1-1;

                for(j=0;j<msgnum;j++){

                    msg[total+j]=pos1[j+1];
                }
                total +=msgnum;
                msg[total]='\n';
		    pos1 = pos2;
                total++;

            }
            msgnum = total;
            msg[msgnum]=0x00;
		//printf("\nCASE4:pos1[%x], pos2[%x], msgnum[%d]",pos1, pos2,msgnum);
            break;

        case 5://Power Save Configuration
	      /*
	     for(index=0;index<len;index++)
		{
			printf("[%2X] ",msg[index]);
			if(index%16 == 0) printf("\n");
		}
		*/
		pos1 = strchr(msg,0x0A);
            for(i=0;i<7;i++){        
                pos2 = strchr(pos1+1,0x0A);
                msgnum = pos2 -pos1-1;
		   // printf("\n [%d] - msgnum[%d], pos1[%d], pos2[%d]", i, msgnum, pos1, pos2);
                for(j=0;j<msgnum;j++){

                    msg[total+j]=pos1[j+1];
               	     }
                total +=msgnum;
                msg[total]='\n';
                pos1 = pos2;
		    total++;

            	}
            msgnum = total;
            msg[msgnum]=0x00;
		//printf("\nCASE5:pos1[%x], pos2[%x], msgnum[%d]",pos1, pos2,msgnum);
            break;


        default:
            break;
    }
    	
    return msgnum;
}

int getIntnum(long val){

    int i=0;

    do{

        val=val/10;
        i++;
    }
    while(val>0);

    return i;
}

int getHexnum(long val){

    int i=0;

    do{

        val=val/16;
        i++;
    }
    while(val>0);

    return i;
}


